<template>
    <el-dialog :visible.sync="is_create_dialog_show" :title="is_create ? '添加职位' : '修改职位'" :before-close="handleClose">
        <el-form :model="role_form" ref="role_form" :rules="rules" label-width="100px" label-position="left" style="width: 400px">
            <el-form-item label="职位名称：" prop="name" v-if="is_create">
                <el-input v-model="role_form.name"></el-input>
            </el-form-item>
            <el-form-item label="职位名称：" prop="roleName" v-else>
                <el-input v-model="role_form.roleName"></el-input>
            </el-form-item>
            <el-form-item label="备注：" prop="remark">
                <el-input type="textarea" v-model="role_form.remark"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button @click="handleClose">取消</el-button>
                <el-button type="primary" @click="onSubmit('role_form')">确定</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import { mapGetters } from "vuex";
import role from "@/api/role.js";

export default {
    name: "create-role",
    computed: {
        ...mapGetters("role", ["is_create_dialog_show", "is_create"])
    },
    props: ["is_show", "form"],
    data() {
        return {
            role_form: {
                roleId: undefined,
                roleName: undefined,
                remark: undefined,
                name: undefined
            },
            rules: {
                roleName: [
                    { required: true, message: "必填", trigger: "blur" }
                ],
                name: [{ required: true, message: "必填", trigger: "blur" }],
                remark: [{ required: false }]
            }
        };
    },
    methods: {
        handleClose() {
            this.$store.commit("role/CHANGE_IS_CREATE_DIALOG_SHOW", false);
        },
        onSubmit(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    if (this.is_create) {
                        role
                            .createRole(this.role_form)
                            .then(res => {
                                this.successCallBack('添加成功!')
                            })
                            .catch(err => {
                                this.$message.error(err)
                            });
                    } else {
                        role.editRole(this.role_form).then(res => {
                            this.successCallBack('编辑成功!')
                        }).catch(err => {
                            this.$message.error(err)
                        })
                    }
                } else {
                    return false;
                }
            });
        },
        successCallBack(message) {
            this.$message({
                type: "success",
                message: message
            });
            this.$store.commit("role/CHANGE_IS_CREATE_DIALOG_SHOW", false);
            this.$store.dispatch("role/getRoleList");
        }
    },
    watch: {
        is_create(newVal) {
            if (newVal) {
                this.role_form = {
                    roleId: undefined,
                    name: undefined
                };
            } else {
                this.role_form = this.form
            }
        },
        form(newVal) {
            this.role_form = newVal
        }
    }
};
</script>

<style>
</style>
